Page({
    data: {
        caseData: null,
        tempFileUrl: null,
        fileType: null
    },

    onLoad(options) {
        const caseItem = JSON.parse(decodeURIComponent(options.item));
        this.setData({
            caseData: caseItem
        });


    },
    // 返回上一页
    goBack() {
        wx.navigateBack();
    },

    // 复制下载链接
    downloadTemplate() {
        const that = this
        wx.showActionSheet({
            itemList: ['复制PDF简历模板链接', '复制Word简历模板链接'],
            success(res) {
                const fileType = res.tapIndex === 0 ? 'pdf' : 'word'
                const fileID = fileType === 'pdf'
                    ? that.data.caseData.fileidPdf
                    : that.data.caseData.fileid

                if (!fileID) {
                    wx.showToast({
                        title: '模板文件不存在',
                        icon: 'none'
                    })
                    return
                }

                wx.showLoading({
                    title: '获取下载链接中...',
                    mask: true
                })

                wx.cloud.callFunction({
                    name: 'getResumeUrl',
                    data: { fileID },
                    success(res) {
                        if (res.result.code === 0) {
                            const url = res.result.data.url
                            wx.setClipboardData({
                                data: url,
                                success: () => {
                                    wx.showModal({
                                        title: '复制成功',
                                        content: `下载链接已复制到剪贴板\n\n链接2分钟后失效，请及时下载\n\n文件名: ${that.data.caseData.title}.${fileType}`,
                                        showCancel: false,
                                        confirmText: '知道了'
                                    })
                                },
                                fail: (err) => {
                                    wx.showToast({
                                        title: '复制失败: ' + (err.errMsg || '未知错误'),
                                        icon: 'none'
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: res.result.message || '获取下载链接失败',
                                icon: 'none'
                            })
                        }
                    },
                    fail(err) {
                        wx.showToast({
                            title: '获取下载链接失败',
                            icon: 'none'
                        })
                        console.error(err)
                    },
                    complete() {
                        wx.hideLoading()
                    }
                })
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })
    }
});